export function filterListData() {
	let dataCache = []

	/**
	 * 设置被筛选的数据
	 * @param {Object[]} data 被筛选的数据
	 */
	function setData(data) {
		dataCache = data
	}

	/**
	 * 筛选数据
	 * @param {String} keyword  筛选的关键字
	 * @param {String} key      数据的 key 值
	 * @returns 
	 */
	function filter(keyword, key) {
		keyword = keyword.trim()

		if (keyword) {
			const reg = new RegExp(keyword, 'i')

			return dataCache.reduce((result, item) => {
				if (reg.test(item[key])) {
					result.push(item)
				}

				return result
			}, [])
		}

		return dataCache
	}

	/**
	 * 销毁筛选器
	 */
	function destroy() {
		dataCache = null
	}

	return {
		setData,
		filter,
		destroy
	}
}


/**
 * 节流函数
 */
export function throttle (fn, delay) {
  let timer = null
  return function (...args) {
    if (timer) {
      return
    }

    timer = setTimeout(() => {
      fn.apply(this, args)
      timer = null
    }, delay)
  }
}

/**
 * 防抖函数
 */
export function debounce (fn, delay) {
  let timer
  return function (...args) {
    if (timer) {
      clearTimeout(timer)
    }

    timer = setTimeout(() => {
      fn.apply(this, args)
      timer = null
    }, delay)
  }
}